#-*- coding: utf-8 -*-
'''
Created on Aug 27, 2012

@author: bbg
'''

from sqlalchemy import Integer, Column, MetaData, Table
from sqlalchemy.orm import mapper
    
class ConfigCookRelation(object):

    _Mapper_ = None
    _company_id_ = -1
    _shop_id_ = -1    
    def __init__(self):
        self.dish_id = 0
        self.cook_id = 0
        self.id = 0
        
    @staticmethod
    def doMapping(company_id, shop_id):
        '''
        按company_id分库，shop_id对应公司分店，每次生成对象前都要执行一次，以便map到正确的数据库上去
        '''
        if ConfigCookRelation._company_id_ == company_id and ConfigCookRelation._shop_id_ == shop_id:
            return
                
        if ConfigCookRelation._Mapper_ is not None:
            ConfigCookRelation._Mapper_.dispose()
            
        metaData = MetaData()
        configCookRelationTable = Table('config_cook_relation', metaData,
                           Column('dish_id', Integer, primary_key = True),
                           Column('cook_id', Integer, primary_key = True),
                           Column('id', Integer, primary_key = True),
                           schema = "shop_%s_%s" % (company_id, shop_id))

        
        ConfigCookRelation._Mapper_ = mapper(ConfigCookRelation, configCookRelationTable)
        